/*
 * GClub.java
 * Created on 18 juin 2007
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2007, Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.club;

import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.GAbstractDao;
import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.assoc.GNationalAssociation;
import com.hockeo.vhbase.data.dao.contest.GLeague;
import com.hockeo.vhbase.data.dao.user.GUser;

/**
 * Represents a single hockey club.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "m_club" )
public class GClub extends GAbstractDao implements Comparable<GClub>
{
  @Id
  @Column( name = "id" )
  private String               d_strId;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "association_id" )
  private GNationalAssociation d_association;

  @Column( name = "country" )
  private String               d_strCountry;

  @Column( name = "name" )
  private String               d_strName;

  @Column( name = "longitude" )
  private double               d_dLongitude;

  @Column( name = "latitude" )
  private double               d_dLatitude;

  /**
   * National seeding position. Determined by the last season's national league results.
   * The champion of the top level league has the first seeding position and the last team
   * of the lowest category league has the last seeding position.
   */
  @Column( name = "seeding" )
  private int                  d_nSeeding;

  @Column( name = "image032_id" )
  private int                  d_nImage032Id;

  @Column( name = "image100_id" )
  private int                  d_nImage100Id;

  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "user_id" )
  private GUser                d_user;

  /**
   * The national league the club is currently affiliated with. Each club must be
   * affiliated to a leage at all times.
   */
  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "league_id" )
  private GLeague              d_league;

  @OneToOne( fetch = FetchType.LAZY )
  @PrimaryKeyJoinColumn
  private GClubStats           d_stats;

  /** Default parameterless constructor. */
  protected GClub()
  {}

  /**
   * Assigns this club to the specified league.
   *
   * @param league the league to which the club should be assigned
   * @throws IllegalArgumentException if the league belongs to another association than
   *           the club
   */
  public void assignToLeague( GLeague league )
  {
    if ( !getAssociation().equals( league.getAssociation() ) )
      throw new IllegalArgumentException( "A club can only be assigned to a league belonging to the same association."
          + "This is not the case for the club " + getId() + " and the league " + league.getId() );

    d_league = league;
  }

  public int getSeeding()
  {
    return d_nSeeding;
  }

  public void setSeeding( int nSeeding )
  {
    d_nSeeding = nSeeding;
  }

  public GLeague getLeague()
  {
    return d_league;
  }

  public GClubStats getStats()
  {
    return d_stats;
  }

  void setStats( GClubStats stats )
  {
    d_stats = stats;
  }

  public GUser getUser()
  {
    return d_user;
  }

  public void setUser( GUser user )
  {
    d_user = user;
  }

  public GNationalAssociation getAssociation()
  {
    return d_association;
  }

  public String getCountry()
  {
    return d_strCountry;
  }

  public String getCountry( Locale locale )
  {
    return translate( d_strCountry, locale );
  }

  public String getName()
  {
    return d_strName;
  }

  public double getLongitude()
  {
    return d_dLongitude;
  }

  public double getLatitude()
  {
    return d_dLatitude;
  }

  public int getImage032Id()
  {
    return d_nImage032Id;
  }

  public int getImage100Id()
  {
    return d_nImage100Id;
  }

  public String getId()
  {
    return d_strId;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GClub.class;
  }

  /**
   * Compares this club to the given club alphabetically by name.
   *
   * @see java.lang.Comparable#compareTo(java.lang.Object)
   */
  public int compareTo( GClub clubOther )
  {
    return getName().compareTo( clubOther.getName() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_strId.equals( ( (GClub) obj ).getId() );
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_strId.hashCode();
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return d_strId + " (" + d_strName + ")";
  }
}
